package com.atguigu.network;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author yihu
 * @create 2021-06-15 14:25
 * @email:1057202547@qq.com
 * @dec:
 * 从客户端发送文件给服务端，服务端保存到本地。并返回“发送成功”给客户端。
 * 并关闭相应的连接。
 */
public class TCPTest3 {
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        InputStream serverIs = null;
        ByteArrayOutputStream bos = null;
        try {
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress,8891);
            os = socket.getOutputStream();
            fis = new FileInputStream("randomAccessFile2.png");

            byte[] buffte = new byte[1024];
            int len;
            while ((len = fis.read(buffte))!=-1){
                os.write(buffte,0,len);
            }
            //关闭数据的传输，通知服务端数据发送完毕
            socket.shutdownOutput();


            //接收来自服务端的数据，并响应到客户端
            serverIs = socket.getInputStream();
            bos = new ByteArrayOutputStream();
            byte[] serverbuffte = new byte[1024];
            int serverlen;
            while ((serverlen = serverIs.read(serverbuffte))!=-1){
                bos.write(serverbuffte,0,serverlen);
            }
            System.out.println("收到来自服务的返回" + bos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverIs!=null){
                try {
                    serverIs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //应该用try catch finally 次处偷懒
    @Test
    public void server() throws IOException {
        ServerSocket ss2 = new ServerSocket(8891);
        Socket socket = ss2.accept();
        InputStream is = socket.getInputStream();
        FileOutputStream fis = new FileOutputStream(new File("clinet2.png"));

        byte[] buffte = new byte[1024];
        int len;
        while ((len = is.read(buffte))!=-1){
            fis.write(buffte,0,len);
        }

        //返回客户端
        System.out.println("收到传输成功.........");

        OutputStream os = socket.getOutputStream();
        os.write("{\"code\":200,\"msg\":\"成功\"}".getBytes());

        ss2.close();
        socket.close();
        is.close();
        fis.close();
        os.close();
    }
}
